<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="8">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>总需求数</span>
            </div>
          </template>
          <div class="card-item">
            <div class="card-item-number">{{ statistics.orderCount }}</div>
            <div class="card-item-text">当前系统中的总需求数量</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>已完成需求</span>
            </div>
          </template>
          <div class="card-item">
            <div class="card-item-number">{{ statistics.completedOrderCount }}</div>
            <div class="card-item-text">已完成的陪护需求数量</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>护工数</span>
            </div>
          </template>
          <div class="card-item">
            <div class="card-item-number">{{ statistics.nurseCount }}</div>
            <div class="card-item-text">当前护工数量</div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-card style="margin-top: 20px">
      <template #header>
        <div class="card-header">
          <span>最近需求列表</span>
          <div>
            <el-input
              v-model="searchForm.nurseName"
              placeholder="护工姓名"
              style="width: 200px; margin-right: 10px"
            />
            <el-input
              v-model="searchForm.patientName"
              placeholder="患者姓名"
              style="width: 200px; margin-right: 10px"
            />
            <el-button type="primary" @click="getStatistics">搜索</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </div>
        </div>
      </template>
      <el-table :data="recentRequests" style="width: 100%">
        <el-table-column prop="orderId" label="需求ID" width="100" />
        <el-table-column prop="patientName" label="患者姓名" width="120" />
        <el-table-column prop="nurseName" label="护工姓名" width="120" />
        <el-table-column prop="paymentStatus" label="状态">
          <template #default="scope">
            <el-tag
              :type="getStatusType(scope.row.paymentStatus)"
            >
              {{ getStatusText(scope.row.paymentStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createdTime" label="创建时间" />
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button size="small" type="primary" @click="viewDetail(scope.row)"
              >查看详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="detailDialogVisible" title="陪护需求详情" width="500px">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="需求ID">{{ detailData.orderId }}</el-descriptions-item>
        <el-descriptions-item label="患者姓名">{{ detailData.patientName }}</el-descriptions-item>
        <el-descriptions-item label="护工姓名">{{ detailData.nurseName }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ getStatusText(detailData.paymentStatus) }}</el-descriptions-item>
        <el-descriptions-item label="开始时间">{{ detailData.startTime }}</el-descriptions-item>
        <el-descriptions-item label="结束时间">{{ detailData.endTime }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detailData.createdTime }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button @click="detailDialogVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { getStatisticsList } from '@/api/statistics'

const statistics = reactive({
  orderCount: 0,
  completedOrderCount: 0,
  nurseCount: 0
})

const searchForm = reactive({
  nurseName: '',
  patientName: ''
})

const recentRequests = ref([])
const detailDialogVisible = ref(false)
const detailData = ref({})

const getStatusType = (status) => {
  const statusMap = {
    1: 'warning',  // 申请中
    2: 'danger',   // 已拒绝
    3: 'primary',  // 进行中
    4: 'success',  // 已完成
    5: 'info'      // 已取消
  }
  return statusMap[status] || 'info'
}

const getStatusText = (status) => {
  const statusMap = {
    1: '申请中',
    2: '已拒绝',
    3: '进行中',
    4: '已完成',
    5: '已取消'
  }
  return statusMap[status] || '未知状态'
}

const viewDetail = (row) => {
  detailData.value = row
  detailDialogVisible.value = true
}

const getStatistics = async () => {
  try {
    const res = await getStatisticsList(searchForm)
    if (res.code === 200 && res.data) {
      statistics.orderCount = res.data.orderCount
      statistics.completedOrderCount = res.data.completedOrderCount
      statistics.nurseCount = res.data.nurseCount
      recentRequests.value = res.data.rows || []
    }
  } catch (e) {
    console.error('获取统计数据失败:', e)
    statistics.orderCount = 0
    statistics.completedOrderCount = 0
    statistics.nurseCount = 0
    recentRequests.value = []
  }
}

const resetSearch = () => {
  searchForm.nurseName = ''
  searchForm.patientName = ''
  getStatistics()
}

onMounted(() => {
  getStatistics()
})
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.box-card {
  height: 100%;
}

.card-item {
  text-align: center;
  padding: 20px 0;
}

.card-item-number {
  font-size: 36px;
  font-weight: bold;
  color: #409eff;
  margin-bottom: 10px;
}

.card-item-text {
  color: #666;
}

.chart-container {
  margin-top: 20px;
  background-color: #f5f7fa;
  padding: 20px;
  border-radius: 4px;
}
</style>
